package com.java.framework;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.io.FileNotFoundException;
import java.io.IOException;

public class FtpTest {

	private void upload(FTPClient client) {
		try {
			client.upload(new java.io.File("localFile.png"),
					new FTPDataTransferListener() {

						@Override
						public void transferred(int arg0) {
							// TODO Auto-generated method stub
							System.out.println("upload === " + arg0);
						}

						@Override
						public void started() {
							// TODO Auto-generated method stub
							System.out.println("upload start");
						}

						@Override
						public void failed() {
							// TODO Auto-generated method stub
							System.out.println("upload failed");
						}

						@Override
						public void completed() {
							// TODO Auto-generated method stub
							System.out.println("upload complete");
						}

						@Override
						public void aborted() {
							// TODO Auto-generated method stub
							System.out.println("upload aborted");

						}
					});
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPIllegalReplyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPDataTransferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPAbortedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				client.disconnect(true);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FTPIllegalReplyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FTPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void download(FTPClient client) {
		try {
			client.download("IMG_20130329_112044.png", new java.io.File(
					"localFile.png"), new FTPDataTransferListener() {

				@Override
				public void transferred(int arg0) {
					// TODO Auto-generated method stub
					System.out.println("download === " + arg0);
				}

				@Override
				public void started() {
					// TODO Auto-generated method stub
					System.out.println("download start");
				}

				@Override
				public void failed() {
					// TODO Auto-generated method stub
					System.out.println("download failed");
				}

				@Override
				public void completed() {
					// TODO Auto-generated method stub
					System.out.println("download complete");
				}

				@Override
				public void aborted() {
					// TODO Auto-generated method stub
					System.out.println("download aborted");

				}
			});
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPIllegalReplyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPDataTransferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPAbortedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				client.disconnect(true);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FTPIllegalReplyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FTPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void connectFtp() {
		try {
			FTPClient client = new FTPClient();
			client.connect("manatee.vicp.cc");
			// client.setAutoNoopTimeout(5000L);
			client.login("mshare", "star");
			System.out.println(" login  " + client.isConnected());
			// download(client);
			upload(client);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPIllegalReplyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		FTPClient client = new FTPClient();
		try {
			client.connect("manatee.vicp.cc");
			client.setAutoNoopTimeout(15000L);
			client.login("mshare", "star");
			System.out.println(" login  " + client.isConnected());
			client.disconnect(true);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPIllegalReplyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FTPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
